package com.wanxi.service.impl;


import com.wanxi.feign.UserFeignClient;
import com.wanxi.mapper.UserMapper;
import com.wanxi.entity.User;
import com.wanxi.result.CommonResult;
import com.wanxi.vo.*;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "用户服务")
@RestController
@RequestMapping("/user")
public class UserServiceImpl implements UserFeignClient {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/addUser")
    @Override
    public CommonResult addUser(UserFrontDTO user) {
//        logger.info("[]",user.getPhoneNumber(),user.getAddress());
        logger.info("入参",user);
        Integer i = userMapper.addUser(user);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @PostMapping("/perfectInformation")
    @Override
    public CommonResult perfectInformation(UserInfoDTO user) {
        logger.info("入参",user);
        Integer i = userMapper.perfectInformation(user);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @GetMapping("/findSettledStatus")
    @Override
    //查询入驻状态接口
    public CommonResult findSettledStatus(Long id) {
        logger.info("入参",id);
        List<DoctorDTO> doctors = userMapper.findSettledStatus(id);
        logger.info("出参",doctors);
        return CommonResult.success(doctors);
    }

    @PostMapping("/SwitchDoctor")
    @Override
    public CommonResult SwitchDoctor(Long id) {
        logger.info("入参",id);
        List<User> users = userMapper.SwitchDoctor(id);
        logger.info("出参",users);
        return CommonResult.success(users);
    }

    @GetMapping("/getUserFront")
    @Override
    public CommonResult getUserFront(UserQuery userQuery) {
        logger.info("入参",userQuery);
        List<UserDTO> users = userMapper.getUserFront(userQuery);
        logger.info("出参",users);
        return CommonResult.success(users);
    }

    @PostMapping("/login")
    @Override
    public CommonResult login(String phoneNumber, String password, String code) {
        logger.info("入参",phoneNumber,password,code);
        User u = userMapper.getUserByphoneNumber(phoneNumber);
        logger.info("出参",u);
        if(!password.equals(u.getPassword())){
            return CommonResult.success("登录成功");
        }
        return CommonResult.failed("登录失败");
    }

    @PutMapping("/updatePassword")
    @Override
    public CommonResult updatePassword(Long id, String password, String newPassword) {
        logger.info("入参",id,password,newPassword);
        Integer i = userMapper.updatePassword(id, newPassword);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @PostMapping("/addUserRole")
    @Override
    public CommonResult addRole(Long id, Integer roleId) {
        logger.info("入参",id,roleId);
        Integer i = userMapper.addRole(id, roleId);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @GetMapping("/getGuardianByCondition")
    @Override
    public CommonResult getGuardianByCondition(UserQuery userQuery) {
        logger.info("入参",userQuery);
        List<User> users = userMapper.getGuardianByCondition(userQuery);
        logger.info("出参",users);
        return CommonResult.success(users);
    }

    @PutMapping("/updateGuardianState")
    @Override
    public CommonResult updateGuardianState(Long id, Integer state) {
        logger.info("入参",id,state);
        Integer i = userMapper.updateGuardianState(id, state);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }
}
